<template>
  <div style="margin-left: 10px">
    <strong style="display: inline-block; margin: 0 0 20px; ">订单管理</strong>
    <!-- 通过订单状态进行筛选 -->
    <div class="search">
      <el-select v-model="optionValue" size="mini" placeholder="类型">
        <el-option
          v-for="item in OptionValueList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button style="margin-left:10px" size="mini">请选择</el-button>
    </div>
    <el-table :data="orderList" style="width: 100%">
      <el-table-column prop="id" label="订单Id"></el-table-column>
      <el-table-column prop="title" label="自行车"></el-table-column>
      <el-table-column prop="username" label="租赁用户"></el-table-column>
      <el-table-column prop="loseTime" label="逾期时间">
        <template slot-scope="scope">
          <div style="color:red" v-show="scope.row.loseTime > 0">{{scope.row.loseTime}}</div>
          <div v-show="scope.row.loseTime === 0">无逾期</div>
        </template>
      </el-table-column>
      <el-table-column prop="createdAt" label="创建时间"></el-table-column>
      <el-table-column prop="status" label="状态" align="left">
        <template slot-scope="scope">
          <div>
            <el-button type="success" plain size="mini" v-show="scope.row.status === 0">租赁中</el-button>
            <el-button type="success" size="mini" v-show="scope.row.status === 1">已还车</el-button>
            <el-button type="danger" size="mini" v-show="scope.row.status === 2">已逾期</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="action" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="editOrderStatus(scope.row.id)"
          >修改状态</el-button>
          <el-button
            type="primary"
            icon="el-icon-delete"
            size="mini"
            @click="deleteOrder(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="选择状态"
      :visible.sync="slectStatusFlag"
      width="30%"
      :before-close="handleCloseStatus"
      :append-to-body="true"
    >
      <el-radio v-model="status" label="0">租赁中</el-radio>
      <el-radio v-model="status" label="1">已还车</el-radio>
      <el-radio v-model="status" label="2">逾期</el-radio>
      <span slot="footer" class="dialog-footer">
        <el-button @click="slectStatusFlag = false">取 消</el-button>
        <el-button type="primary" @click="sureStatus">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAllOrders, reviseOrder, delOrder } from "../../../api/index";
import { formatTime } from "../../../common/js/myFun";
import { statusList } from "../../../data/index";
export default {
  name: "orderManage",
  data() {
    return {
      slectStatusFlag: false,
      optionValue: 0,
      OptionValueList: statusList,
      status: "0",
      orderList: [],
      orderId: ""
    };
  },
  watch: {
    optionValue: function(status) {
      window.console.log(status, "val");
      this.getAllOrders(status);
    }
  },
  mounted() {
    this.getAllOrders(this.optionValue);
  },
  components: {},
  methods: {
    getAllOrders(status) {
      getAllOrders({
        status: status
      }).then(res => {
        if (res.code === 200) {
          let listArr = [];
          res.result.orderList.forEach(item => {
            // item.bikeInfo.status = item.orderInfo.status;
            window.console.log(item.userInfo.id, "item.userInfo.id");
            item.orderInfo.username = item.userInfo.username;
            item.orderInfo.title = item.bikeInfo
              ? item.bikeInfo.title
              : "此车已被删除";
            item.orderInfo.createdAt = formatTime(item.orderInfo.createdAt);
            listArr.push(item.orderInfo);
          });
          this.orderList = listArr;
          window.console.log(listArr, "listArr");
        } else {
          this.$message({
            message: res.data,
            type: "error"
          });
        }
      });
    },
    handleCloseStatus() {
      this.slectStatusFlag = false;
    },
    editOrderStatus(id) {
      this.slectStatusFlag = true;
      this.orderId = id;
    },
    sureStatus() {
      reviseOrder({
        orderId: this.orderId,
        status: parseInt(this.status)
      }).then(res => {
        if (res.code === 200) {
          this.$message({
            message: res.data,
            type: "success"
          });
          this.handleCloseStatus();
          this.getAllOrders(this.optionValue);
          // this.optionValue = parseInt(this.status)
        } else {
          this.$message({
            message: res.data,
            type: "error"
          });
        }
      });
    },
    deleteOrder(id) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delOrder({ id: id }).then(res => {
            if (res.code === 200) {
              this.$message({
                message: res.data,
                type: "success"
              });
              this.getAllOrders(this.optionValue);
            } else {
              this.$message({
                message: res.data,
                type: "错误"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped lang="stylus" ref="stylesheet/stylus">
.search {
  margin: 0px 0 30px;
}
</style>
